﻿Imports System.Data.SqlClient
Public Class frmUniformes
    Dim uni As New clsUniformes
    Private mstrValor As String
    Dim cont As Integer
    Dim cont2 As Integer
    Dim limite As Integer
    Dim Bitacora As New clsActividad

    Public ReadOnly Property Valor() As String
        Get
            Valor = mstrValor
        End Get
    End Property
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub frmUniformes_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ToolStrip2.Cursor = Cursors.Hand
        ToolStrip2.RenderMode = System.Windows.Forms.ToolStripRenderMode.System

        Me.dgUniformes.DefaultCellStyle.Font = New Font("Arial", 8, FontStyle.Regular)

        cont = 0
        cont2 = 0
        limite = frmModuloEmpleados.dgEmpleados.Rows.Count - 1

        Dim EmpleadoSelect As String = BuscarStrDatoCitra("Empleados", "NombreCompleto", "IdEmpleado=" & EmpleadoID)

        If EmpleadoSelect = "" Then
            Me.lblNombre.Text = "Seleccionar un Empleado"
        Else
            Me.lblNombre.Text = EmpleadoSelect
            cargauniformes()

        End If
        limpiaCampos()
        Me.txtID.Text = (BuscarIntDatoCitra("SELECT MAX(IdUniforme) as Id FROM Uniformes")) + 1
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


    End Sub
    Sub limpiaCampos()
        Me.datFecha.Value = Now()
        Me.txtCC.Text = 0
        Me.txtCP.Text = 0
        Me.txtTP.Text = ""
        Me.txtTC.Text = ""


    End Sub
    Public Sub gatter()
        uni.idUniforme = Me.txtID.Text
        uni.Fecha = Me.datFecha.Value
        uni.TallaPantalon = Me.txtTP.Text
        uni.CantidadPantalon = Me.txtCP.Text
        uni.TallaCamisa = Me.txtTC.Text
        uni.CantidadCamisa = Me.txtCC.Text
        uni.TallaGorro = ""
        uni.CantidadGorro = 0
        uni.idEpleado = Trim(EmpleadoID)

    End Sub
    Sub cargauniformes()
        dgUniformes.Rows.Clear()
        Dim cnConn As New sqlConnection
        cnConn.ConnectionString = CitraConnection


        Dim strSql As String = ""

        strSql = "select *"
        strSql = strSql & " from Uniformes where idempleado=" & Trim(EmpleadoID)
        strSql = strSql & " order by fecha"


        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = strSql
        Dim rdBuscar As SqlDataReader
        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader

        Dim cont As Integer = 0
        Dim Fecha As Date


        Do While rdBuscar.Read()
            cont = cont + 1
            Fecha = rdBuscar("fecha")
            dgUniformes.Rows.Add(rdBuscar("idUniforme"), Fecha.ToShortDateString, rdBuscar("tallapantalon"), rdBuscar("cantidadpantalon"), rdBuscar("tallacamisa"), rdBuscar("cantidadcamisa"))
        Loop
    End Sub
    Private Sub dgResult_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgUniformes.CellClick

        Dim strRetVal As String = ""

        Try
            strRetVal = CStr(dgUniformes.Item(0, dgUniformes.CurrentCell.RowIndex).Value)
        Catch ex As Exception
            strRetVal = ""
        Finally
            mstrValor = strRetVal.Trim
        End Try

        Dim strResult As String
        strResult = Me.Valor

        If strResult <> "" Then
            uni.idUniforme = Trim(strResult)
            uni.Buscar()
            scatter()

        End If
    End Sub
    Private Sub dgResult_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgUniformes.KeyDown

        If e.KeyCode = Keys.Enter Then
            Dim strRetVal As String = ""

            Try
                strRetVal = CStr(dgUniformes.Item(0, dgUniformes.CurrentCell.RowIndex).Value)
            Catch ex As Exception
                strRetVal = ""
            Finally
                mstrValor = strRetVal.Trim
                Dim strResult As String
                strResult = Me.Valor

                If strResult <> "" Then
                    uni.idUniforme = Trim(strResult)
                    uni.Buscar()
                    scatter()

                End If
            End Try
        End If
    End Sub
    Private Sub scatter()
        Me.txtCC.Text = uni.CantidadCamisa
        Me.txtCP.Text = uni.CantidadPantalon
        Me.txtTC.Text = uni.TallaCamisa
        Me.txtTP.Text = uni.TallaPantalon
        Me.datFecha.Value = uni.Fecha
        Me.txtID.Text = uni.idUniforme
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub toolGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub
    Sub Excel()
        Dim xl As Object
        Dim wb As Object
        Dim i As Integer
        xl = CreateObject("Excel.Application")
        xl.Visible = True
        xl.Workbooks.Add()
        wb = xl.activeworkbook


        wb.SHEETS(1).cells(1, 1).value = "Imprimiendo..."
        wb.SHEETS(1).columns(1).columnwidth = 15

        Dim rng As Integer = 5

        For i = 2 To 6
            wb.SHEETS(1).cells(rng, i).FONT.name = "Segoe UI Light"
            wb.SHEETS(1).cells(rng, i).font.bold = True
            wb.SHEETS(1).CELLS(rng, i).HorizontalAlignment = -4108
            wb.SHEETS(1).CELLS(rng, i).VerticalAlignment = -4108
            wb.SHEETS(1).CELLS(rng, i).WrapText = True
            wb.SHEETS(1).CELLS(rng, i).Orientation = 0
            wb.SHEETS(1).CELLS(rng, i).AddIndent = False
            wb.SHEETS(1).CELLS(rng, i).IndentLevel = 0
            wb.SHEETS(1).CELLS(rng, i).ShrinkToFit = False
            wb.SHEETS(1).CELLS(rng, i).MergeCells = False
            wb.SHEETS(1).CELLS(rng, i).ReadingOrder = -5002
        Next
        wb.SHEETS(1).cells(rng, 2).value = "Fecha"
        wb.SHEETS(1).cells(rng, 3).value = "Talla Pantalon"
        wb.SHEETS(1).cells(rng, 4).value = "Cantidad"
        wb.SHEETS(1).cells(rng, 5).value = "Talla Camisa"
        wb.SHEETS(1).cells(rng, 6).value = "Cantidad"
        wb.SHEETS(1).columns(2).columnwidth = 12
        wb.SHEETS(1).columns(3).columnwidth = 12
        wb.SHEETS(1).columns(4).columnwidth = 12
        wb.SHEETS(1).columns(5).columnwidth = 12
        wb.SHEETS(1).columns(6).columnwidth = 12

        rng = rng + 1
        For i = 0 To dgUniformes.Rows.Count - 1
            For j = 2 To 6
                wb.SHEETS(1).cells(rng, j).FONT.name = "Segoe UI Light"

            Next
            wb.SHEETS(1).cells(rng, 2).value = dgUniformes.Rows(i).Cells(1).Value
            wb.SHEETS(1).cells(rng, 3).value = dgUniformes.Rows(i).Cells(2).Value
            wb.SHEETS(1).cells(rng, 4).value = dgUniformes.Rows(i).Cells(3).Value
            wb.SHEETS(1).cells(rng, 5).value = dgUniformes.Rows(i).Cells(4).Value
            wb.SHEETS(1).cells(rng, 6).value = dgUniformes.Rows(i).Cells(5).Value

            rng = rng + 1
        Next

        Dim EmpleadoSelect As String = BuscarStrDatoCitra("Empleados", "NombreCompleto", "IdEmpleado=" & EmpleadoID)

        wb.SHEETS(1).cells(1, 2).value = EmpresaActiva
        wb.SHEETS(1).cells(1, 2).FONT.SIZE = 20
        wb.SHEETS(1).cells(1, 2).FONT.name = "Segoe UI Light"
        wb.SHEETS(1).cells(2, 2).value = "Historial de Uniformes de " & EmpleadoSelect
        wb.SHEETS(1).cells(2, 2).FONT.SIZE = 14
        wb.SHEETS(1).cells(2, 2).FONT.name = "Segoe UI Light"
        wb.SHEETS(1).cells(3, 2).value = "Impreso el " & Now().Day & "-" & Now().Month & "-" & Now().Year & " por " & UsuarioActivo
        wb.SHEETS(1).cells(3, 3).FONT.SIZE = 12
        wb.SHEETS(1).cells(3, 3).FONT.name = "Segoe UI Light"

        wb.SHEETS(1).cells(1, 1).value = ""
        wb.SHEETS(1).columns(1).columnwidth = 3
        wb.sheets(1).cells(1, 1).interior.color = 15773696
        wb.sheets(1).cells(2, 1).interior.color = 15773696
    End Sub
    Private Sub toolSalir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub ToolStripButton8_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton8.Click
        Dim EmpleadoSelect As String = BuscarStrDatoCitra("Empleados", "NombreCompleto", "IdEmpleado=" & EmpleadoID)
        Dim EmpleadoStatus As String = BuscarStrDatoCitra("Empleados", "EstadoEmpleado", "IdEmpleado=" & EmpleadoID)

        If Trim(EmpleadoStatus = "B") Then
            MsgBox("No puedes agregar ni modificar registros porque el empleado es Baja", MsgBoxStyle.Exclamation, "Aviso")
            Exit Sub
        End If
        If EmpleadoSelect = "" Then
            MsgBox("Debes indicar un empleado", MsgBoxStyle.Exclamation, "Aviso")
            Exit Sub
        End If
        Dim concatenado As String = Me.txtTC.Text & Me.txtTP.Text
        Dim cantidad As Integer = Me.txtCC.Text + Me.txtCP.Text
        If concatenado = "" Then
            MsgBox("Debes alguna talla", MsgBoxStyle.Exclamation, "Aviso")
            Exit Sub
        End If
        If cantidad < 1 Then
            MsgBox("Debes alguna cantidad", MsgBoxStyle.Exclamation, "Aviso")
            Exit Sub
        End If
        gatter()
        uni.Guardar()
        cargauniformes()
        Bitacora.RegistraActividad("Registró entrega de uniformes a " & EmpleadoSelect)

        limpiaCampos()
        Me.txtID.Text = (BuscarIntDatoCitra("SELECT MAX(IdUniforme) as Id FROM Uniformes")) + 1
    End Sub

    Private Sub ToolStripButton6_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton6.Click
        Excel()
    End Sub

    Private Sub ToolStripButton5_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton5.Click
        cont2 = cont2 + 1
        If ((valorRowSelect.Index + cont) - (cont2)) < 0 Then
            MsgBox("o hay mas registros para mostrar del filtro", MsgBoxStyle.Exclamation, "Aviso")
            cont2 = cont2 - 1
            Exit Sub
        End If

        EmpleadoID = frmModuloEmpleados.dgEmpleados.Rows((valorRowSelect.Index + cont) - (cont2)).Cells(0).Value.ToString.Trim

        Dim EmpleadoSelect As String = BuscarStrDatoCitra("Empleados", "NombreCompleto", "IdEmpleado=" & EmpleadoID)
        Me.lblNombre.Text = EmpleadoSelect
        limpiaCampos()
        cargauniformes()
    End Sub

    Private Sub ToolStripButton7_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton7.Click
        cont = cont + 1
        If cont - cont2 > limite Then
            MsgBox("o hay mas registros para mostrar del filtro", MsgBoxStyle.Exclamation, "Aviso")
            cont = cont - 1
            Exit Sub
        End If
        EmpleadoID = frmModuloEmpleados.dgEmpleados.Rows((valorRowSelect.Index + cont) - (cont2)).Cells(0).Value.ToString.Trim

        Dim EmpleadoSelect As String = BuscarStrDatoCitra("Empleados", "NombreCompleto", "IdEmpleado=" & EmpleadoID)
        Me.lblNombre.Text = EmpleadoSelect
        limpiaCampos()
        cargauniformes()
    End Sub

    Private Sub ToolStripButton4_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton4.Click
        Close()
    End Sub
End Class